package com.mail;

import org.junit.Test;

import java.security.Security;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.net.InetAddress;
import java.util.Date;
import java.util.Properties;
import java.security.Security;
public class SendEmail {
    // mail server


    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private final static String Keys = "着急哦";
    private String host = "smtp.exmail.qq.com";
    private String port = "465";
    //	private String host = "smtp.163.com";
    // email account
    private String username = "yu.wu@mayihr.com";
    // password
    private String password = "Weipin#2018";

    private String mail_head_name = Keys;

    private String mail_head_value = Keys;

    private String[] mailTo = {"1405201500@qq.com", "yu.wu@mayihr.com"};
    private String[] mailCc = {"auto_testers@163.com",};
    private String mail_from = username;
    private String mail_subject = Keys;
    StringBuffer mail_body = new StringBuffer();
    private String personalName = "ZhaoGang Test Automation";

    public SendEmail() {
    }

    /**
     * Send email
     */

    public void send(String emailSubject)
            throws Exception {

        try {
            String reportLink = "";

            InetAddress inet = InetAddress.getLocalHost();
            reportLink = inet.getHostAddress();
//			reportLink = "file://" + reportLink + ""
//					+ folderPath.replace("D:\\Selenium\\Project\\", "\\");
            // reportLink="\\\\"+reportLink+"\\"+folderPath.replace(":", "$");
            System.out.println("reportLink" + reportLink);
            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator(); // mail server
            // authentication
            props.put("mail.smtp.host", host);

            // Rance remark it at 4/12, don't know why,just know it works
            // without below sentence.
            props.put("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.setProperty("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(props, auth);

            // Set session
            MimeMessage message = new MimeMessage(session);
            // message.setContent("foobar, "application/x-foobar");
            message.setSubject(emailSubject);
            // message.setSubject(mail_subject); //set mail subject

            mail_body
                    .append("<DIV dir=ltr align=left><SPAN class=821095908-26102009><FONT face='Arial'\r\n");
            mail_body
                    .append("color=#0000ff size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r\n");
            mail_body
                    .append("</FONT></SPAN><IMG id=Picture_x0020_2 height=92\r\n");
            // mail_body.append("alt=\"QA Automation Header_final.bmp\" src=\"http://10.197.12.107/MercerAutomationFramework/QA%20Automation%20Header_final.bmp\"\r\n");
            mail_body.append("width=595 border=0><o:p></o:p></DIV>\r\n");
            mail_body.append("<DIV class=Section1>\r\n");
            mail_body
                    .append("<BLOCKQUOTE style=\"MARGIN-TOP: 5pt; MARGIN-BOTTOM: 5pt; MARGIN-RIGHT: 0mm\">\r\n");
            mail_body
                    .append("  <TABLE class=MsoNormalTable style=\"BORDER-COLLAPSE: collapse\" cellSpacing=0 \r\n");
            mail_body.append("  cellPadding=0 border=0>\r\n");
            mail_body.append("    <TBODY>\r\n");
            mail_body.append("    <TR>\r\n");
            mail_body.append("      <TD \r\n");
            mail_body
                    .append("        <P class=MsoNormal style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">This is to inform you that your Test Automation run has been completed successfully. Please find below a summary of the test results<o:p></o:p></P>\r\n");
            mail_body.append("    <TR>\r\n");
            mail_body.append("      <TD \r\n");
            mail_body.append("      vAlign=top width=595>\r\n");
            mail_body
                    .append("        <TABLE class=MsoNormalTable style=\"WIDTH: 96.36%\" cellPadding=0 \r\n");
            mail_body.append("          <TBODY>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: black; PADDING-BOTTOM: 0.75pt; WIDTH: 99.28%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"99%\" colSpan=2>\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; COLOR: white; FONT-FAMILY: 'Arial','sans-serif'\">Summary</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD></TR>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #e8ffe8; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Total \r\n");
            mail_body.append("              Tests Passed</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #e8ffe8; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");
            // passed this.Total_PassNumber

            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + 0
                            + "<o:p></o:p></SPAN></P></TD></TR>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffe6ff; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Total \r\n");
            mail_body.append("             Tests Failed</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffe6ff; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");
            // failed this.Total_FailNumber

            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + 0
                            + "<o:p></o:p></SPAN></P></TD></TR>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffff99; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Total \r\n");
            mail_body.append("              Tests Warned</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffff99; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");
            // warned this.Total_Warning_number

            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + 0
                            + "<o:p></o:p></SPAN></P></TD></TR>\r\n");
            /*
             * mail_body.append("          <TR>\r\n");
             * mail_body.append("            <TD \r\n"); mail_body.append(
             * "            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n"
             * ); mail_body.append("            width=\"22%\">\r\n");
             * mail_body.append
             * ("              <P class=MsoNormal><B><SPAN \r\n");
             * mail_body.append(
             * "              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Executed \r\n"
             * ); mail_body.append("              On</SPAN></B><SPAN \r\n");
             * mail_body.append(
             * "              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n"
             * ); mail_body.append("            <TD \r\n"); mail_body.append(
             * "            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n"
             * ); mail_body.append("            width=\"76%\">\r\n");
             * mail_body.append("              <P class=MsoNormal><SPAN \r\n");
             * //execTime String execTime="1233444"; mail_body.append(
             * "              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
             * + execTime + "<o:p></o:p></SPAN></P></TD></TR>\r\n");
             */
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Start \r\n");
            mail_body.append("              Time</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("           <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");

            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + " \r\n");
            mail_body
                    .append("              <o:p></o:p></SPAN></P></TD></TR>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">End \r\n");
            mail_body.append("              Time</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");
            // endTime
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + " \r\n");
            mail_body
                    .append("              <o:p></o:p></SPAN></P></TD></TR>\r\n");
            mail_body.append("          <TR>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 22.88%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"22%\">\r\n");
            mail_body.append("              <P class=MsoNormal><B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'\">Execution \r\n");
            mail_body.append("              Time</SPAN></B><SPAN \r\n");
            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P></TD>\r\n");
            mail_body.append("            <TD \r\n");
            mail_body
                    .append("            style=\"PADDING-RIGHT: 0.75pt; PADDING-LEFT: 0.75pt; BACKGROUND: #ffffdc; PADDING-BOTTOM: 0.75pt; WIDTH: 76.06%; PADDING-TOP: 0.75pt\" \r\n");
            mail_body.append("            width=\"76%\">\r\n");
            mail_body.append("              <P class=MsoNormal><SPAN \r\n");
            // execDuration

            mail_body
                    .append("              style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">"
                            + "<o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE>\r\n");
            mail_body.append("    <TR>\r\n");
            mail_body.append("      <TD \r\n");
            mail_body.append("      vAlign=top width=595>\r\n");
            mail_body.append("        <br>   \r\n");
            mail_body
                    .append("        <P class=MsoNormal style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">To view the complete report, please click on this \r\n");
            mail_body.append("        link<o:p></o:p></P>\r\n");
            // reportLink

            mail_body
                    .append("        <P class=MsoPlainText style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><A href=\""
                            + reportLink
                            + "\">"
                            + reportLink
                            + "</A><o:p></o:p></P></TD></TR>\r\n");
            mail_body.append("    <TR>\r\n");
            mail_body.append("      <TD \r\n");
            mail_body.append("      vAlign=top width=595>\r\n");
            mail_body
                    .append("        <P class=MsoNormal><o:p>&nbsp;</o:p></P></TD></TR>\r\n");
            mail_body.append("    <TR>\r\n");
            mail_body.append("      <TD \r\n");
            mail_body.append("      vAlign=top width=595>\r\n");
            mail_body
                    .append("        <P class=MsoNormal style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\">Thank you<o:p></o:p></P></TD></TR></TBODY></TABLE>\r\n");
            mail_body.append("  <P class=MsoNormal \r\n");
            mail_body
                    .append("  style=\"mso-margin-top-alt: auto; mso-margin-bottom-alt: auto\"><SPAN \r\n");
            mail_body
                    .append("  style=\"FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Arial','sans-serif'\">Best \r\n");
            mail_body.append("  Regards,</SPAN><SPAN \r\n");
            mail_body
                    .append("  style=\"FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','serif'\"><o:p></o:p></SPAN></P>\r\n");
            mail_body.append("  <P class=MsoNormal><SPAN \r\n");
            mail_body
                    .append("  style=\"FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Arial','sans-serif'\">Automation Testing Team<BR>ZhaoGang<BR>US | Shanghai | UK</SPAN></P>\r\n");
            mail_body.append("  <P \r\n");
            mail_body.append("  class=MsoNormal>\r\n");
            mail_body.append("  <o:p></o:p></P>\r\n");
            // mail_body.append("  <P class=MsoNormal><IMG id=Picture_x0020_1 height=76 alt=\"QA Automation Footer.bmp\" src=\"http://10.197.12.107/MercerAutomationFramework/QA%20Automation%20Footer.bmp\" \r\n")
            mail_body.append("  <DIV>\r\n");

            BodyPart mdp = new MimeBodyPart();// new BodyPart instance to save
            // the content
            mdp.setContent(mail_body.toString(), "text/html;charset=gb2312");// set
            // the
            // display
            // format
            Multipart mm = new MimeMultipart();
            mm.addBodyPart(mdp);
            message.setContent(mm);

            // message.setText(mail_body.toString());
            message.setHeader(mail_head_name, mail_head_value);
            // message.setHeader(emailSubject, emailSubject);

            message.setSentDate(new Date());
            // Address address = new InternetAddress(mail_from, personalName);
            Address address = new InternetAddress(mail_from, emailSubject);
            message.setFrom(address);
            // single to address
            // Address toAddress = new InternetAddress(mailTo);
            // message.addRecipient(Message.RecipientType.TO, toAddress);

            // ***************************************************mutilple to
            // address
            String toList = getMailList(mailTo);
            InternetAddress[] iaToList = new InternetAddress().parse(toList);

            message.setRecipients(Message.RecipientType.TO, iaToList);
            // **************************************************************
            String ccList = getMailList(mailCc);
            InternetAddress[] iaCcList = new InternetAddress().parse(ccList);
            message.setRecipients(Message.RecipientType.CC, iaCcList);// ������
            Transport.send(message);
            System.out.println("send success!");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception(ex.getMessage());
        }
    }


    public void send2(String emailSubject)
            throws Exception {

        try {
            String reportLink = "";
            InetAddress inet = InetAddress.getLocalHost();
            reportLink = inet.getHostAddress();
            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator(); // mail server
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.setProperty("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props, auth);
            MimeMessage message = new MimeMessage(session);
            message.setSubject(emailSubject);

            BodyPart mdp = new MimeBodyPart();// new BodyPart instance to save
            // the content
            mail_body.append("哟见主题");
            mdp.setContent(mail_body.toString(), "");// set

            Multipart mm = new MimeMultipart();
            mm.addBodyPart(mdp);
            message.setContent(mm);

            message.setHeader(mail_head_name, mail_head_value);
            message.setSentDate(new Date());
            Address address = new InternetAddress(mail_from, emailSubject);
            message.setFrom(address);
            String toList = getMailList(mailTo);
            InternetAddress[] iaToList = new InternetAddress().parse(toList);

            message.setRecipients(Message.RecipientType.TO, iaToList);
            // **************************************************************
            String ccList = getMailList(mailCc);
            InternetAddress[] iaCcList = new InternetAddress().parse(ccList);
            message.setRecipients(Message.RecipientType.CC, iaCcList);// ������
            Transport.send(message);
            System.out.println("send success!");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception(ex.getMessage());
        }
    }


    public class Email_Autherticator extends Authenticator {
        public Email_Autherticator() {
            super();
        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }

    // This is for debugging single function
    private String getMailList(String[] mailArray) {
        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();
    }

    @Test
    public void test() throws Exception {
        SendEmail sendEmail = new SendEmail();
        sendEmail.send2("test");

    }
}
